﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Security.Principal;
using System.Xml.Linq;
using Microsoft.Practices.Unity;
using COM.Service;
using COM.Web;
using SampleApp.Service;
using SampleApp.Data;

namespace SampleApp.Web.Service
{
    interface IAppPermissionRegister
    {
        void RegisterRoleTypes();
        void RegisterRoleActionList();
        void RegisterRoleFileList();
        void RegisterRolePermissionItemList();
    }

    /// <summary>
    /// 注册Action,哪些角色可以调用
    /// </summary>
    public class AppPermissionRegister : IAppPermissionRegister
    {
        public void RegisterRoleActionList()
        {
            var roleActionDataService = Unity.Instance.GetService<IRoleActionDataService>();
            roleActionDataService.EventReload += (sender, e) =>
                {
                    var sysRoleService = Unity.Instance.GetService<ISysRoleService>();
                    var sysMenuService = Unity.Instance.GetService<ISysMenuService>();
                    var roleList = sysRoleService.GetSysRoleList();
                    var menuAll = sysMenuService.GetSysMenuList();
                    foreach (var role in roleList)
                    {
                        var menuIDList = sysRoleService.GetMenuListByRole(role.RoleID);
                        var menuItemList = menuAll.Where(m => menuIDList.Any(id => m.MenuID == id)).ToList();
                        //Controller所有Action权限控制
                        var controllerList = menuItemList.Select(i =>
                            {
                                var mvc = i.Url.Trim('/').Split('/');
                                return new ActionPermission() { HttpMethod = "*", Namespace = mvc[0], Controller = mvc[1], Action = "*" };
                            }).ToList();
                        roleActionDataService.AddRoleAction(role.RoleID, controllerList);
                    }

                    this.RegisterByDev(roleActionDataService);
                };
        }

        private void RegisterByDev(IRoleActionDataService roleAction)
        {
            
            //登录的任何用户可以查看自己的用户信息
            roleAction.AddRoleAction<WebUserPermissionRole>(new List<ActionPermission>
            {
                new ActionPermission(){HttpMethod="Get",Namespace="SysModule",Controller="UserInfo",Action = "*"},
                new ActionPermission(){HttpMethod="Get",Namespace="SysModule",Controller="SysUser",Action = "ReadOnly"},
            });

            roleAction.AddRoleAction<COM.Data.AnyPermissionRole>(new List<ActionPermission>
            {
                new ActionPermission(){HttpMethod="Get",Namespace="SysModule",Controller="SysUser",Action = "Role"},
            });
        }

        public void RegisterRoleTypes()
        {
            var permissionRoleListService = Unity.Instance.GetService<IPermissionRoleListService>();
            permissionRoleListService
                 .RegisterRoleType<AppEveryOne>()
                 .RegisterRoleType<AppAdmin>()
                 .RegisterRoleType<AppEmployee>()
                 .RegisterRoleType<AppDeptLeader>()
                 .RegisterRoleType<AppCompLeader>()
                 .RegisterRoleType<AppTest>()
                 .RegisterRoleType<AppDev>()
                 ;
        }

        public void RegisterRoleFileList()
        {
            var roleFileDataService = Unity.Instance.GetService<IRoleFileDataService>();
            roleFileDataService.EventReload += (sender, e) =>
            {
                roleFileDataService.AddRoleFile<DevPermissionRole>(new List<FilePermission>()
                {
                        new FilePermission()
                        {
                            HttpMethod="*",
                            FileName = "TestDataService1.svc",
                        },
                        new FilePermission()
                        {
                            HttpMethod="*",
                            FileName = "TestDataService2.svc",
                        },
                });
            };
        }

        public void RegisterRolePermissionItemList()
        {
             
            //var rolePermissionDataService = Unity.Instance.GetService<IRolePermissionDataService>();
            //rolePermissionDataService.EventReload += (sender, e) =>
            //{
            //    rolePermissionDataService.AddRolePermission<AppDev>(new List<string>()
            //    {
            //        EnumPermissionName.DeleteLogEntity, // "删除日志"
            //    });
            //};
        }

    }

}


